class Question {
  final int id;
  final String title;
  final int rank;
  final int type;
  final String op1;
  final String op2;
  final String op3;
  final String op4;
  final int titleType;
  final String titlePic;

  Question({
    required this.id,
    required this.title,
    required this.rank,
    required this.type,
    required this.op1,
    required this.op2,
    required this.op3,
    required this.op4,
    required this.titleType,
    required this.titlePic,
  });

  factory Question.fromJson(Map<String, dynamic> json) {
    return Question(
      id: json['id'],
      title: json['title'],
      rank: json['rank'],
      type: json['type'],
      op1: json['op1'],
      op2: json['op2'],
      op3: json['op3'] ?? '',
      op4: json['op4'] ?? '',
      titleType: json['titleType'],
      titlePic: json['titlePic'] ?? '',
    );
  }

  // 获取选项列表
  List<String> get options {
    List<String> opts = [op1, op2];
    if (op3.isNotEmpty) opts.add(op3);
    if (op4.isNotEmpty) opts.add(op4);
    return opts;
  }

  // 获取题目类型描述
  String get questionTypeText {
    switch (titleType) {
      case 1:
        return '单选题';
      case 2:
        return '判断题';
      case 3:
        return '多选题';
      default:
        return '未知类型';
    }
  }

  // 获取考试类型描述
  String get examTypeText {
    return type == 1 ? '科目一' : '科目四';
  }

  // 获取车辆类型描述
  String get vehicleTypeText {
    switch (rank) {
      case 1:
        return '小车';
      case 2:
        return '货车';
      case 3:
        return '卡车';
      case 4:
        return '摩托车';
      default:
        return '未知';
    }
  }

  // 添加方法
  bool matchesSearchQuery(String query) {
    if (query.isEmpty) return true;

    // 搜索ID
    if (id.toString() == query) return true;

    // 搜索题目内容
    if (title.toLowerCase().contains(query.toLowerCase())) return true;

    return false;
  }
}
